package com.hundsun.documentripper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.dto.DocumentTemplateSource;
import com.hundsun.documentripper.domain.pojo.DocumentTemplate;
import com.hundsun.documentripper.domain.pojo.DocumentTemplateRelation;
import com.hundsun.documentripper.domain.query.DocumentTemplateQuery;
import com.hundsun.documentripper.mapper.DocumentTemplateMapper;
import com.hundsun.documentripper.service.DocumentTemplateRelationService;
import com.hundsun.documentripper.service.DocumentTemplateService;
import com.hundsun.documentripper.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class DocumentTemplateServiceImpl implements DocumentTemplateService {

    @Autowired
    private DocumentTemplateMapper documentTemplateMapper;

    @Autowired
    private DocumentTemplateRelationService documentTemplateRelationService;

    @Override
    public DocumentTemplate insertDocumentTemplate(DocumentTemplate record) {
        record.setUuid(UUID.randomUUID().toString());
        documentTemplateMapper.insert(record);
        return record;
    }

    @Override
    public void updateDocumentTemplate(DocumentTemplate record) {
        documentTemplateMapper.updateById(record);
    }

    @Override
    public void deleteDocumentTemplate(String uuid) {
        DocumentTemplate record = documentTemplateMapper.selectById(uuid);
        documentTemplateMapper.deleteById(uuid);
        documentTemplateRelationService.deleteDocumentTemplateRelationByCode(record.getDocTemplateCode());
    }

    @Override
    public IPage<DocumentTemplate> getDocumentTemplateListByPage(DocumentTemplateQuery baseQuery) {
        QueryWrapper<DocumentTemplate> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtil.isNotEmpty(baseQuery.getDocTemplateName()),"doc_template_name",baseQuery.getDocTemplateName());
        return documentTemplateMapper.selectPage(new Page<DocumentTemplate>(baseQuery.getCurrent(), baseQuery.getSize()), wrapper);
    }

    @Override
    public DocumentTemplateSource getDocumentTemplateById(String uuid) {
        DocumentTemplate documentTemplate = documentTemplateMapper.selectById(uuid);
        DocumentTemplateSource documentTemplateSource = DocumentTemplateSource.formDocumentTemplate(documentTemplate);
        documentTemplateSource.setRelationViewList(documentTemplateRelationService.getAllDocumentTemplateRelationByCode(documentTemplate.getDocTemplateCode()));
        return documentTemplateSource;
    }

    @Override
    public DocumentTemplateSource setNodeTemplate(DocumentTemplateSource documentTemplateSource) {
        //删除原来设置的节点模板
        documentTemplateRelationService.deleteDocumentTemplateRelationByCode(documentTemplateSource.getDocTemplateCode());
        //设置新的节点模板
        for(DocumentTemplateRelation relation : documentTemplateSource.getRelationList()){
            relation.setUuid(UUID.randomUUID().toString());
            relation.setDocTemplateCode(documentTemplateSource.getDocTemplateCode());
            relation.setCreateUser(documentTemplateSource.getCreateUser());
        }
        documentTemplateRelationService.insertDocumentTemplateRelationBatch(documentTemplateSource.getRelationList());
        return documentTemplateSource;
    }
}
